package displayboardinfo.tests;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

import displayboardinfo.entities.RegisteredForUpdates;
import displayboardinfo.entities.Room;
import displayboardinfo.entities.User;
import displayboardinfo.messages.ListRoomsRes;

public class RegisterTest {
	public static void main(String[] args) {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());
		
		RegisteredForUpdates registeredForUpdates = new RegisteredForUpdates();
		registeredForUpdates.setIp("localhost");
		registeredForUpdates.setPort(7171);
		
		ClientResponse res = service.path("rooms").path("list").type(MediaType.APPLICATION_XML).get(ClientResponse.class);
		Room room = res.getEntity(ListRoomsRes.class).getRooms().get(0);
		
		registeredForUpdates.setRoom(room);
		
		try {
			JAXBContext context = JAXBContext.newInstance(RegisteredForUpdates.class);
			Marshaller m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			m.marshal(registeredForUpdates, System.out);
		} catch (PropertyException e) {
			e.printStackTrace();
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		
		
		try{
			res = service.path("updates").path("register").type(MediaType.APPLICATION_XML).post(ClientResponse.class, registeredForUpdates);
			
			if (res.getStatus() == ClientResponse.Status.NOT_ACCEPTABLE.getStatusCode()){
				System.err.println("Not acceptable");
				System.err.println(res.getEntity(String.class));
			}
			else {
				System.out.println(res);
			}
		
		} catch (Exception e){
			System.err.println("Exception");
		}
		
	}
	
	private static URI getBaseURI() {
		return UriBuilder.fromUri(
				"http://localhost:8080/DisplayBoardInfoServer").build();
	}
}
